package com.xxgc.eow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xxgc.eow.utils.CosUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.xxgc.eow.result.Result;
import java.util.List;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xxgc.eow.service.ITopiclibraryService;
import com.xxgc.eow.entity.Topiclibrary;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 题库 前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2023-05-29
 */
@Tag(name = "题库")
@RestController
@RequestMapping("/topiclibrary")
public class TopiclibraryController {

    @Resource
    private ITopiclibraryService topiclibraryService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "查询所有接口", description = "查询题库所有数据")
    @SaIgnore
    @GetMapping
    public Result<List<Topiclibrary>> findAll() {
        return Result.ok(topiclibraryService.list());
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "根据id查询数据接口", description = "通过组件id查询")
    @Parameters({
            @Parameter(name = "id",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/{id}")
    public Result<Topiclibrary> findOne(@PathVariable Integer id) {
        return Result.ok(topiclibraryService.getById(id));
    }

    /**
     * 分页查询接口
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "分页查询接口", description = "分页查询")
    @Parameters({
            @Parameter(name = "pageNum",description = "页码",example="1",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "pageSize",description = "每页多少条",example="10",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/page")
    public Result<Page<Topiclibrary>> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        QueryWrapper<Topiclibrary> queryWrapper = new QueryWrapper<>();
        return Result.ok(topiclibraryService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 新增和更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "新增和更新接口", description = "传入id为更新,不传为新增,时间和有效字段均不填")
    @SaIgnore
    @PostMapping
    public Result save(@RequestBody Topiclibrary topiclibrary) {
        topiclibraryService.saveOrUpdate(topiclibrary);
        return Result.ok();
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "删除接口", description = "通过id删除")
    @Parameters({
            @Parameter(name = "id",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        topiclibraryService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "批量删除接口", description = "批量删除接口")
    @SaIgnore
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        topiclibraryService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 获取banner图接口
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "获取banner图接口", description = "获取banner图接口")
    @SaIgnore
    @GetMapping("/banners")
    public Result getBanners() {
        return Result.ok(topiclibraryService.getBanners());
    }

    /**
     * 通过id获取名称
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "获取题库名称接口", description = "通过id获取题库名称接口")
    @SaIgnore
    @GetMapping("/libraryName")
    public Result getNameByLibraryId(Integer libraryId) {
        return Result.ok(topiclibraryService.getTopicLibraryNameByLibraryId(libraryId));
    }

    /**
     * 模糊查询题库
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "模糊查询题库", description = "模糊查询题库")
    @SaIgnore
    @GetMapping("/libraryMsg")
    public Result getAllByMsg(String msg) {
        List<Topiclibrary> topiclibraries = topiclibraryService.getTopicLibrarysByMsg(msg);
        if(topiclibraries.size() == 0){
            return Result.error("未查询到相关数据");
        }
        return Result.ok(topiclibraries);
    }

    /**
     * 添加banner图
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "添加banner图", description = "添加banner图")
    @SaIgnore
    @GetMapping("/addBanner")
    public Result saveBanner(MultipartFile file) {
        return Result.ok(CosUtils.uploadFile(file,"images/banners/"));
    }

    /**
     * 添加题库封面
     * @return
     */
    @ApiOperationSupport(author = "yxh")
    @Operation(summary = "添加题库封面", description = "添加题库封面")
    @SaIgnore
    @GetMapping("/addImg")
    public Result saveImg(MultipartFile file) {
        return Result.ok(CosUtils.uploadFile(file,"images/library/"));
    }
}